﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;

/// <summary>
/// Summary description for CFile
/// </summary>
public class CFile
{
	public CFile()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public bool Add(string title,int parent,string isclass)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object, object> pars = new Dictionary<object, object>();
        pars.Add("Parent", parent);
        object obj = d.ExeScalar("select count(*) from file where parent=@Parent", pars);
        pars.Add("Title", title);
        pars.Add("SortCode", Convert.ToInt32(obj) + 1);
        pars.Add("IsClass", isclass);
        int c = d.ExeNonQuery("insert into file(parent,title,sortcode,isclass) values(@Parent,@Title,@SortCode,@IsClass)", pars);
        d.Close();
        if (c > 0)
        {
            return true;
        }
        return false;
    }

    public bool Update(int id,string title)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object, object> pars = new Dictionary<object, object>();
        pars.Add("Title", title);
        pars.Add("ID", id);
        int c = d.ExeNonQuery("update file set title=@Title where id=@ID", pars);
        d.Close();
        if (c > 0)
        {
            return true;
        }
        return false;
    }

    public bool Delete(int id)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object, object> pars = new Dictionary<object, object>();
        pars.Add("ID", id);
        DataTable dt = d.ExeDataTable("select * from file where ID=@ID", pars);
        int c = d.ExeNonQuery("delete from file where id=@ID", pars);
        pars.Clear();
        pars.Add("Parent", Convert.ToInt32(dt.Rows[0]["parent"].ToString()));
        pars.Add("SortCode", Convert.ToInt32(dt.Rows[0]["SortCode"].ToString()));
        c += d.ExeNonQuery("update file set sortcode=sortcode-1 where parent=@Parent and sortcode > @SortCode", pars);
        d.Close();
        if (c > 0)
        {
            return true;
        }
        return false;
    }

    public bool Sort(int id,bool isUp)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object, object> pars = new Dictionary<object, object>();
        pars.Add("ID", id);
        DataTable dt = d.ExeDataTable("select * from file where id=@ID", pars);
        pars.Clear();
        pars.Add("Parent", Convert.ToInt32(dt.Rows[0]["parent"].ToString()));
        pars.Add("SortCode", Convert.ToInt32(dt.Rows[0]["sortcode"].ToString()));
        if (isUp)
        {
            dt = d.ExeDataTable("select top 1 * from file where  parent=@Parent and sortcode<@SortCode order by sortcode desc", pars);
        }
        else
        {
            dt = d.ExeDataTable("select top 1 * from file where  parent=@Parent and sortcode >@SortCode order by sortcode asc", pars);
        }
        pars.Clear();
        pars.Add("ID", id);
        int c = d.ExeNonQuery("update file set sortcode=" + (isUp ? "sortcode-1" : "sortcode+1") + " where id=@ID", pars);
        pars.Clear();
        if (null != dt)
        {
            pars.Add("ID", Convert.ToInt32(dt.Rows[0]["id"].ToString()));
            c += d.ExeNonQuery("update file set sortcode=" + (isUp ? "sortcode+1" : "sortcode-1") + " where id=@ID", pars);
        }
        d.Close();
        if (c > 0)
        {
            return true;
        }
        d.Close();
        return false;
    }

    public bool SetPath(int id, string path,Page p)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object, object> pars = new Dictionary<object, object>();
        pars.Add("ID", id);
        DataTable dt = d.ExeDataTable("select * from file where ID=@ID", pars);
        if (null != dt && dt.Rows[0]["path"].ToString() != "")
        {
            try
            {
                if (System.IO.File.Exists(p.Server.MapPath("~/" + dt.Rows[0]["path"].ToString())))
                {
                    System.IO.File.Delete(p.Server.MapPath("~/" + dt.Rows[0]["path"].ToString()));
                }
            }
            catch (Exception)
            { }
        }

        pars.Clear();
        pars.Add("Path", path);
        pars.Add("ID", id);
        int c = d.ExeNonQuery("update file set [path]=@Path where id=@ID", pars);
        d.Close();
        if (c > 0)
        {
            return true;
        }
        return false;
    }

    public DataTable ReadFiles(int parent)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object, object> pars = new Dictionary<object, object>();
        pars.Add("Parent", parent);
        DataTable dt = d.ExeDataTable("select * from file where parent=@Parent order by sortcode asc", pars);
        d.Close();
        return dt;
    }

    public DataRow ReadFile(int id)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object, object> pars = new Dictionary<object, object>();
        pars.Add("ID", id);
        DataTable dt = d.ExeDataTable("select * from file where ID=@ID", pars);
        d.Close();
        if (null != dt)
        {
            return dt.Rows[0];
        }
        return null;
    }
}